﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Text;
using JinianNet.JNCms.Data;

namespace JinianNet.JNCms.SQLiteDAL
{
    public class Classify : BaseDAL, IClassify
    {

        public int Insert(Models.Classify model)
        {
            if (model.ID <= 0)
                model.ID = Db.CreateKey("Classify");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@Title", DbType.String,200),
					new SQLiteParameter("@ID", DbType.Int32,4),
					new SQLiteParameter("@ClassType", DbType.Int32,4),
                    new SQLiteParameter("@UserID", DbType.Int32,4),
					new SQLiteParameter("@CreateTime", DbType.DateTime),
					new SQLiteParameter("@ParentID", DbType.Int32,4)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.ID;
            parameters[2].Value = model.ClassType;
            parameters[3].Value = model.UserID;
            parameters[4].Value = model.CreateTime;
            parameters[5].Value = model.ParentID;
            return Db.Helper.ExecuteNonQuery(CommandType.Text, "insert into JNCms_Classify ([Title],[ID],[ClassType],[UserID],[CreateTime],[ParentID]) values (@Title,@ID,@ClassType,@UserID,@CreateTime,@ParentID)", parameters);
        }

        public int Update(Models.Classify model)
        {
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@Title", DbType.String,200),
					new SQLiteParameter("@ID", DbType.Int32,4)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.ID;
            return Db.Helper.ExecuteNonQuery(CommandType.Text, "update JNCms_Classify set [Title]=@Title where [ID]=@ID", parameters);
        }

        public int Delete(int id)
        {
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4)};

            parameters[0].Value = id;
            return Db.Helper.ExecuteNonQuery(CommandType.Text, "delete from JNCms_Classify where [ID]=@ID", parameters);
        }

        public List<Models.Classify> GetList(int parent, int classType)
        {
            List<Models.Classify> list = new List<Models.Classify>();
            GetList(list, parent, classType);
            return list;
        }

        private void GetList(List<Models.Classify> list, int parent, int classType)
        {
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ClassType", DbType.Int32,4),
					new SQLiteParameter("@ParentID", DbType.Int32,4)};
            parameters[0].Value = classType;
            parameters[1].Value = parent;
            using (IDataReader rd = Db.Helper.ExecuteReader(CommandType.Text, "select * from JNCms_Classify where [ClassType]=@ClassType and [ParentID]=@ParentID Order by [CreateTime] desc ", parameters))
            {
                while (rd.Read())
                {
                    list.Add(db.Entity.PopulateEntity<Models.Classify>(rd));
                    GetList(list, list[list.Count - 1].ID, classType);
                }
            }
        }

        public Models.Classify GetItem(int id)
        {
            using (IDataReader rd = Db.Helper.ExecuteReader(CommandType.Text, "select * from [JNCms_Classify] where [ID]=@ID limit 1", new SQLiteParameter("@ID", id)))
            {
                if (rd.Read())
                {
                    return Db.Entity.PopulateEntity<Models.Classify>(rd);
                }
            }
            return null;
        }
    }
}
